<?php

namespace app\common\model\option;

use app\common\model\User;
use think\Model;


class Log extends Model
{





    // 表名
    protected $name = 'option_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'createtime_text',
        'type_text'
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function options()
    {
        return $this->belongsTo('Option', 'option_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public static function getOptionType()
    {
        return [
            100 => '每日YS释放',
            200 => 'YS互转',
            300 => 'YS兑换',
            301 => 'YS兑换失败返还',
            400 => 'YS互换手续费',
        ];
    }
    /**
     * Notes:记录会员账户流水，如果变动类型是成长值，且是增加的，则调用更新会员等级方法。该方法应在添加用户账户后调用
     * @author: cjh 2020/12/15 11:49
     * @param int $user_id  用户id
     * @param float $amount 变动数量
     * @param int $change_type  变动类型：1-减少；2-增加
     * @param int $source_type 来源类型
     * @param string $source_id 来源id
     * @param string $source_sn 来源单号
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function income($user_id, $amount, $change_type, $source_type, $source_sn = '', $option_id = 0)
    {
        $user = new User();
        $user = $user->get($user_id);
        if (empty($user)) {
            return false;
        }

        $before = $user->options;
        if ($change_type == 1) {
            $user->setDec('options', $amount);

            $amount = -$amount;
        } else {
            $user->setInc('options', $amount);
        }
        $list =  self::getOptionType();
        $left_amount = $user->options;
        $account_log = new self;
        $data['type'] = $change_type;
        $data['order_sn'] = $source_sn;
        $data['user_id'] = $user_id;
        $data['option_id'] = $option_id;
        $data['option'] = $amount ?? 0;
        $data['before'] = $before ?? 0;
        $data['after'] = $left_amount ?? 0;
        $data['memo'] = isset($list[$source_type]) ? $list[$source_type] : '';
        $data['source_type'] = $source_type;
        $account_log->allowField(true)->save($data);
        return true;
    }

    
}
